//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#if !defined (_BATTERY_H_)
#define _BATTERY_H_

#define DUMP_OUT(x) x
#define BAT_DBG(x) //x

#define BAT_CAP_INTERVAL 10

#define BAT_LOWBAT      0x1
#define BAT_CHGINSERT   0x2
#define BAT_CHGREMOVE   0x3

#define USB_CHARGER     0x4
#define WALL_CHARGER    0x5

#define RECHARGE_VOL_DEF    4178
#define SOFT_LOW_BAT_CAP    3600

//Battery Charge Mode
#define MODE_QUALIFICATION   0x00// qualification mode
#define MODE_PRECHARGE       0x01// pre charge mode
#define MODE_TRICKLE         0x02// trickle charge mode
#define MODE_FASTCCCV        0x03// fast charge mode (CCCV)
#define MODE_FASTNOCC        0x04// fast charge mode (no CC)
#define MODE_FASTNOCV        0x05// fast charge mode (no CV)
#define MODE_SWITCH          0x06// switch mode (no CC, no CV)
#define MODE_IDLE            0x07// idle mode

//Battery Threshold voltage
#define BTV_000 0x0    //n.a.
#define BTV_280 0x1    //2.80 V
#define BTV_290 0x2    //2.90 V
#define BTV_300 0x3    //3.00V
#define BTV_310 0x4    //3.10 V
#define BTV_320 0x5    //3.20 V
#define BTV_330 0x6    //3.30 V
#define BTV_340 0x7    //3.40 V

//Battery Charge Control Voltage
#define CHV_400 0x00 //4.00 V
#define CHV_402 0x01 //4.02 V
#define CHV_404 0x02 //4.04 V
#define CHV_406 0x03 //4.06 V
#define CHV_408 0x04 //4.08 V
#define CHV_410 0x05 //4.10 V
#define CHV_412 0x06 //4.12 V
#define CHV_414 0x07 //4.14 V
#define CHV_416 0x08 //4.16 V
#define CHV_418 0x09 //4.18 V
#define CHV_420 0x0A //4.20 V
#define CHV_422 0x0B //4.22 V
#define CHV_424 0x0C //4.24 V
#define CHV_426 0x0D //4.26 V
#define CHV_428 0x0E //4.28 V
#define CHV_500 0x0F //5.00 V

//Charge Interrupt Register Bit
#define CHGINS_INT      0x01
#define CHGRM_INT       0x02
#define CHGFOK_INT      0x04
#define CHGERR_INT      0x08
#define CHGFRDY_INT     0x10
#define CHGPROT_INT     0x20
#define CHGWD10S_INT    0x40
#define CHGWDEXP_INT    0x80

#define LOWBAT_INT      0x40
#define HIGHTMP_INT     0x80

//Post Events & Status
//Normal
#define PM_CHARGE_INSERT  0x00000001
#define PM_CHARGE_REMOVE  0x00000002
#define PM_FASTCHG_RDY    0x00000004
#define PM_FASTCHG_FIN    0x00000008
#define PM_CHGWD10S_DEC   0x00000010
#define PM_CHGWD_EXP      0x00000020
#define PM_CHARGE_ERR     0x00000040
#define PM_CHARGE_POT     0x00000080
#define PM_SOFT_LOW_BAT   0x00000100
#define PM_BATTERY_CAP    0x00000200
#define PM_BATTERY_WAKE   0x00000400
//Status
#define PM_BAT_LOWVOL     0x80000000
#define PM_BAT_HIGHTMP    0x40000000
#define PM_CHARGE_START   0x20000000
#define PM_CHARGE_STOP    0x10000000
#define PM_CHARGER_WALL   0x08000000
#define PM_CHARGER_USB    0x04000000

//Watch Dog for Charging time
#define WDT_00MIN  0x00
#define WDT_03MIN  0x01
#define WDT_06MIN  0x02
#define WDT_09MIN  0x03
#define WDT_12MIN  0x04
#define WDT_15MIN  0x05
#define WDT_18MIN  0x06
#define WDT_21MIN  0x07
#define WDT_24MIN  0x08
#define WDT_27MIN  0x09
#define WDT_30MIN  0x0A
#define WDT_33MIN  0x0B
#define WDT_36MIN  0x0C
#define WDT_39MIN  0x0D
#define WDT_42MIN  0x0E
#define WDT_45MIN  0x0F
#define WDT_48MIN  0x10
#define WDT_51MIN  0x11
#define WDT_54MIN  0x12
#define WDT_57MIN  0x13
#define WDT_60MIN  0x14
#define WDT_63MIN  0x15
#define WDT_66MIN  0x16
#define WDT_69MIN  0x17
#define WDT_72MIN  0x18
#define WDT_75MIN  0x19
#define WDT_78MIN  0x1A
#define WDT_81MIN  0x1B
#define WDT_84MIN  0x1C
#define WDT_87MIN  0x1D
#define WDT_90MIN  0x1E
#define WDT_93MIN  0x1F

//Led repetition period
#define  LEDPED_04S 0x00//0.4 s 000
#define  LEDPED_10S 0x01//1.0 s 001
#define  LEDPED_12S 0x02//1.2 s 010
#define  LEDPED_20S 0x03//2.0 s 011
#define  LEDPED_26S 0x04//2.6 s 100
#define  LEDPED_40S 0x05//4.0 s 101
#define  LEDPED_60S 0x06//6.0 s 110
#define  LEDPED_80S 0x07//8.0 s 111

//Led pattern
#define ON_050MS 0x00//000 ON = 50 ms, OFF for rest of repetition period
#define ON_100MS 0x01//001 ON = 100 ms, OFF for rest of repetition period
#define ON_200MS 0x02//010 ON = 200 ms, OFF for rest of repetition period
#define ON_500MS 0x03//011 ON= 500 ms, OFF for rest of repetition period
#define OFO050MS 0x04//100 ON = 50 ms, OFF = 50 ms, ON = 50 ms, OFF for rest of repetition period
#define OFO100MS 0x05//101 ON = 100 ms, OFF = 100 ms, ON = 100 ms, OFF for rest of repetition period
#define OFO200MS 0x06//110 ON = 200 ms, OFF = 200 ms, ON = 200 ms, OFF for rest of repetition period
#define ON_INFMS 0x07//111 LED is continuously ON

//Led Value macro
#define LED_PERIOD(x)   (x & 0x07)
#define LED_PATTERN(x)  ((x & 0x07) << 3)
#define LED_ONCHARGE(x) ((x & 0x01) << 6)
#define LED_ONACTIVE(x) ((x & 0x01) << 7)

//Battery reference control macro
#define PCF_DUMP_STATUS                 6677
#define PCF_BATCHARGE_ENABLE            6678
#define PCF_BATCHARGE_DISABLE           6679
#define PCF_SET_RECHARGE_VOL            6680
#define PCF_SET_INFOPOST_INTERVAL       6681
#define PCF_CHANGE_LED_SETTING          6682
#define PCF_NORMAL_LED_SETTING          6683
#define PCF_CHARGE_LED_ON               6684
#define PCF_CHARGE_LED_OFF              6685
#define PCF_NORMAL_LED_ON               6686
#define PCF_NORMAL_LED_OFF              6687
#define PCF_WATCHDOG_RESET              6688

class CBattery : public CDeviceDriver
{
public:
    CARAPI Read(
        /* [in] */ Int64 u64Offset,
        /* [in] */ Int32 bytesToRead,
        /* [out] */ MemoryBuf * pBuffer,
        /* [out] */ IEvent * * ppCompletionEvent);

    CARAPI Write(
        /* [in] */ Int64 u64Offset,
        /* [in] */ const MemoryBuf &buffer,
        /* [out] */ Int32 * pBytesWritten,
        /* [out] */ IEvent * * ppCompletionEvent);

    CARAPI Control(
        /* [in] */ Handle32 nControlCode,
        /* [in] */ const MemoryBuf &inBuffer,
        /* [out] */ MemoryBuf * pOutBuffer,
        /* [out] */ IEvent * * ppCompletionEvent);

    virtual void Dispose(){}

    ECode ChargeMgmThread();
    void EnableAutoVoltageDetect(Boolean IsEn);
    void BatteryReChargeVoltageSetting(UInt32);
    void BatterySetInfoPostInterval(UInt32);
    void LED_ChargeEnable(Boolean IsEnable);
    void LED_NormalEnable(Boolean IsEnable);
    void LED_ChargeStyleSetting(char period, char pattern);
    void LED_NormalStyleSetting(char period, char pattern);
    void ChangeChargeLedSetting(char period, char pattern);
    void ChangeNormalLedSetting(char period, char pattern);
    void Initialize();
    void Calibrate();

    // Member variables
public:
    Boolean    m_bInCharge, m_IsActive;
    DzEvent m_ChargeEvent;
    int     m_VolDetectTM;
    UInt32    m_reChargeVol;
    UInt32    m_InfoPostInterval;
    char    m_ChargeIntStat;
    char    m_BatteryIntStat;
    char    m_SoftDog;
    char    m_ledchgPAT, m_lednorPAT;
    char    m_ledchgPED, m_lednorPED;
    int     m_ChargeOk;
    int     m_IsUSBCharger;
    int     m_delta[4];
};

#endif //_BATTERY_H_
